<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Template\Lib;

use Al\Core\Exception\FileNotFoundException,
    Al\Core\Lib\Service;

/**
 * Minifies a file using the provided backend, employs automatic caching to
 * improve performance.
 * 
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
abstract class AbstractMinify {

  /**
   * @var array The micro-cache metadata array.
   */
  private $meta = array();

  /**
   * Minifies a file.
   *
   * @param string $filename A path to a file.
   * @return string The minified contents of a file.
   * @throws FileNotFoundException
   */
  final public function packFile($filename) {
    if (!is_file($filename)) {
      throw new FileNotFoundException($filename);
    }
    $filename = realpath($filename);
    $hash = md5($filename);
    $cache = Service::get('cache');
    if (isset($cache[$hash]) && !$this->isModified($filename)) {
      return $cache[$hash];
    }
    return $this->pack(file_get_contents($filename));
  }
  
  /**
   * Returns the automatic micro-cache metadata.
   * 
   * @return array The micro-cache metadata array.
   */
  private function &getMeta() {
    if (!$this->meta) $this->meta = Service::get('cache.minify', array());
    return $this->meta;
  }
  
  /**
   * Checks whether the file has been modified since the last check.
   * 
   * @param string $filename The path to a file.
   * @return boolean Has the file been modified?
   */
  private function isModified($filename) {
    $filename = realpath($filename);
    $hash = md5($filename);
    $meta = &$this->getMeta();
    if (!isset($meta[$hash])) {
      $meta[$hash] = array('time' => 0);
      return true;
    }
    if (!isset($meta[$hash]['time'])) {
      $meta[$hash]['time'] = 0;
    }
    $time = filemtime($filename);
    if ($time > $meta[$hash]['time']) {
      $meta[$hash]['time'] = $time;
      return true;
    }
    return false;
  }

  /**
   * Uses a particular backend to minify the file.
   *
   * @param string $source [optional] The original contents of a file.
   * @return string The minified contents of a file.
   */
  abstract public function pack($source = '');
  
}